package tests;

import static org.junit.Assert.assertEquals;

import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Before;
import org.junit.Test;

import controller.UseCaseController;
import controller.UseCaseControllerImplementation;
import controller.data.readDocumentation.ReadDocumentationRequest;
import controller.data.readDocumentation.ReadDocumentationResponse;

public class TestPerformance {

	private static final int THREAD_NUMBER = 100;

	private List<Runnable> listOfRequests;
	private List<List<ReadDocumentationResponse>> listOfResponses;

	@Before
	public void setUp() throws Exception {
		listOfResponses = new Vector<List<ReadDocumentationResponse>>();
		listOfRequests = new Vector<Runnable>();
		for (int i = 0; i < THREAD_NUMBER; i++) {
			listOfRequests.add(new Runnable() {
				UseCaseController controller = new UseCaseControllerImplementation();

				public void run() {
					listOfResponses.add(controller
							.readDocumentation(new ReadDocumentationRequest(
									"specialinfo", "java.util.List")));
				}
			});
		}

	}

	@Test
	public void testPerformance() {
		ExecutorService executor = Executors.newCachedThreadPool();
		for (Runnable runnable : listOfRequests) {
			executor.execute(runnable);
		}
		while (listOfResponses.size() < THREAD_NUMBER) {
		}
		executor.shutdown();

		// Anzahl der Antworten
		assertEquals(THREAD_NUMBER, listOfResponses.size());

		// Anzahl der Datensätze pro Antwort
		for (List<ReadDocumentationResponse> readDocumentationResponses : listOfResponses) {
			assertEquals(25, readDocumentationResponses.size());
		}
	}

	// @Test
	public void testOneThreadPerformance() {
		UseCaseController useCaseController = new UseCaseControllerImplementation();
		List<ReadDocumentationResponse> readDocumentationResponses = useCaseController
				.readDocumentation(new ReadDocumentationRequest("specialinfo",
						"java.util.List"));
		assertEquals(25, readDocumentationResponses.size());
	}
}
